package com.mydomain.debtbook;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.SimpleCursorTreeAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemLongClickListener;

public class EditGroupActivity extends ExpandableListActivity {

	private DebtAdapter debtAdapter;
	private Cursor groupCursor;
	private String groupName;

	private String deleteName;
	private String deleteNameId;

	private ExpandableListAdapter listAdapter;
	private ExpandableListView expandListView;

	private static final int MY_CHILD_DIALOG = 0;
	private static final int MY_GROUP_DIALOG = 1;

	private boolean isNameList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		debtAdapter = ((debtBookApplication) this.getApplication())
				.getDatabaseAdapter();
		isNameList = true;
		initExpandableNameList();

	}

	private void initExpandableNameList() {
		groupCursor = debtAdapter.getAllGroup();

		// listAdapter = new MyExpandableNameListAdapter(groupCursor, this,
		// android.R.layout.simple_expandable_list_item_1,
		// android.R.layout.simple_expandable_list_item_1, new String[] {
		// DebtAdapter._GROUP},
		// new int[] {android.R.id.text1}, new String[] {
		// DebtAdapter._NAME }, new int[] {
		// android.R.id.text1});

		listAdapter = new MyExpandableNameListAdapter(groupCursor, this,
				R.layout.group_group_row, R.layout.group_child_row,
				new String[] { DebtAdapter._GROUP },
				new int[] { R.id.ggr_group }, new String[] { DebtAdapter._ID,
						DebtAdapter._NAME }, new int[] { R.id.gcr_rowId,
						R.id.gcr_child });

		setListAdapter(listAdapter);

		expandListView = getExpandableListView();
		expandListView
				.setOnItemLongClickListener(new OnItemLongClickListener() {
					@Override
					public boolean onItemLongClick(AdapterView<?> parent,
							View v, int position, long id) {
						TextView group = (TextView) v
								.findViewById(R.id.ggr_group);
						TextView child = (TextView) v
								.findViewById(R.id.gcr_child);
						TextView childRow = (TextView) v
								.findViewById(R.id.gcr_rowId);

						if (group == null) {

							deleteNameId = childRow.getText().toString();
							deleteName = child.getText().toString();

							showDialog(MY_CHILD_DIALOG);

						} else {

							deleteName = group.getText().toString();
							showDialog(MY_GROUP_DIALOG);

						}

						return true;
					}
				});

		expandListView.setOnChildClickListener(new OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// TextView t = (TextView) v.findViewById(R.id.cr_rowId);
				// startEdit(t.getText().toString());
				return true;
			}
		});

	}

	public class MyExpandableNameListAdapter extends SimpleCursorTreeAdapter {

		public MyExpandableNameListAdapter(Cursor cursor, Context context,
				int groupLayout, int childLayout, String[] groupFrom,
				int[] groupTo, String[] childrenFrom, int[] childrenTo) {
			super(context, cursor, groupLayout, groupFrom, groupTo,
					childLayout, childrenFrom, childrenTo);
		}

		@Override
		// Bascially for this part, you just need to figure out how to return a
		// children cursor given a group cursor
		protected Cursor getChildrenCursor(Cursor groupCursor) {
			// Given the group, we return a cursor for all the children within
			// that group

			groupName = groupCursor.getString(groupCursor
					.getColumnIndex(DebtAdapter._GROUP));

			Cursor childCursor = debtAdapter.getAllPersonForGroup(groupName);
			return childCursor;

		}

	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case MY_CHILD_DIALOG:

			return new AlertDialog.Builder(getParent())
					.setTitle("Remove Debt")
					.setMessage(
							"Are you sure you want to remove " + deleteName
									+ " from the group?")
					.setPositiveButton(getString(android.R.string.ok),
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int arg1) {

									debtAdapter
											.deleteNameFromGroup(deleteNameId);

									((SimpleCursorTreeAdapter) listAdapter)
											.getCursor().requery();

								}
							})
					.setNegativeButton(getString(android.R.string.cancel),
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int arg1) {
								}
							}).create();
		case MY_GROUP_DIALOG:

			return new AlertDialog.Builder(getParent())
					.setTitle("Remove Debt")
					.setMessage(
							"What would you like to do to the group: "
									+ deleteName)
					.setPositiveButton(getString(R.string.delete),
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int arg1) {

									debtAdapter.deleteGroup(deleteName);

									((SimpleCursorTreeAdapter) listAdapter)
											.getCursor().requery();

								}
							})
					.setNeutralButton(getString(R.string.addMember),
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int arg1) {
									startEdit();

								}
							})
					.setNegativeButton(getString(android.R.string.cancel),
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int arg1) {
								}
							}).create();

		}

		return null;
	}

	@Override
	public void onResume() {
		super.onResume();
		groupCursor.requery();
	}

	private void startEdit() {
		startActivity(new Intent(this, ListNewGroupActivity.class).putExtra(
				"groupName", deleteName));
	}

}
